/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author ThanhTri
 */
public class PSWater extends PSystem {
    protected int stt;
    public PSWater (int _x,int _y) throws IOException{
        super(RSParticle.getRS().getBubble());
        m_XOrigin = _x*30+15;
        m_YOrigin = _y*30+15;
    }
     public void Init (){
         
        m_MaxParticle =18 ;
        stt = 0;
        for (int i = 0;i<m_MaxParticle;i++){
		AddParticle();
                stt = i;
	}
    }
    public void ResetParticle (Particle _particle){
        _particle.m_X = 0;
        _particle.m_Y = 0;
        
        
        
        _particle.m_LifeTime = 0.5f;
        _particle.m_Age = 0;
        _particle.m_iLive = true;
    }
    public void AddParticle (){
        Particle p = new Particle();
        if (stt<9){
            p.m_Vy = (float)Math.sin( stt*Math.PI/4.0f) * 60;
            p.m_Vx = (float)Math.cos( stt*Math.PI/4.0f) * 60; 
        }else {
            p.m_Vy = (float)Math.sin( (stt+0.5f)*Math.PI/4.0f) * 30;
            p.m_Vx = (float)Math.cos( (stt+0.5f)*Math.PI/4.0f) * 30;
        }
        
        ResetParticle(p);
        m_Particles.addElement(p);
    }
    public void Reset (){
        for (int i = 0; i < m_Particles.size(); i++) {
            Particle p= (Particle)m_Particles.elementAt(i);
            ResetParticle(p);
        }
    }
    public void Update (float _Time){
        if(!m_Particles.isEmpty()){
           
            for (int i = 0; i < m_Particles.size(); i++) {
                
                Particle p = (Particle) m_Particles.elementAt(i);
                p.m_Y += p.m_Vy * _Time;
                p.m_X += p.m_Vx * _Time;
                
                p.m_Age += _Time;
                if (p.m_Age>=p.m_LifeTime){
                    ResetParticle(p);
                }
            }
        }
    }
    public void Render (Graphics _g){
        for (int i = 0; i < m_Particles.size(); i++) {
                Particle p = (Particle) m_Particles.elementAt(i);
                _g.drawRegion(m_Image, 0, 0, 4, 4, Sprite.TRANS_NONE,
                        (int)(m_XOrigin+p.m_X), (int)(m_YOrigin+p.m_Y),Graphics.TOP|Graphics.LEFT);
            }
    }
    public void DrawScreen (Graphics _g,GameScreen _Screen){
     for (int i = 0; i < m_Particles.size(); i++) {
                Particle p = (Particle) m_Particles.elementAt(i);
                _g.drawRegion(m_Image, 0, 0, 4, 4, Sprite.TRANS_NONE,
                        (int)(m_XOrigin+p.m_X)- (_Screen.getXView() - _Screen.getWidth()/2),
                        (int)(m_YOrigin+p.m_Y)- (_Screen.getYView() - _Screen.getHeight()/2),Graphics.TOP|Graphics.LEFT);
            }
    }
    public void RemoveDeadParticle (){
        
    }
}
